package com.example.wan_android.ui.fragment

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.wan_android.BR
import com.example.wan_android.R
import com.example.wan_android.adapter.ProjectContentAdapter
import com.example.wan_android.adapter.ProjectTabAdapter
import com.example.wan_android.databinding.FragmentProjectBinding
import com.example.wan_android.tools.SpacesItemDecoration
import com.example.wan_android.tools.SpacesItemEndDecoration
import com.example.wan_android.viewmodel.ProjectViewModel

class ProjectFragment : BaseFragment<FragmentProjectBinding, ProjectViewModel>(
    ProjectViewModel::class.java,
    R.layout.fragment_project,
    BR.viewModel
) {
    private val tabAdapter by lazy {
        ProjectTabAdapter(mViewModel!!)
    }

    private val contentAdapter by lazy {
        ProjectContentAdapter(mViewModel!!)
    }

    override fun initViews() {
        mViewDataBinding?.rvProjectTab?.adapter = tabAdapter
        mViewDataBinding?.rvProjectTab?.layoutManager =
            LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false)
        mViewDataBinding?.rvProjectTab?.addItemDecoration(SpacesItemEndDecoration(15))
        mViewModel?.projectCategoryLiveData?.observe(this) {
            it.onSuccess {
                tabAdapter.setListData(it)
            }
        }
        mViewModel?.isSuccess?.observe(this) {
            if (it) {
                mViewDataBinding?.ivEmpty?.visibility = View.GONE
                mViewDataBinding?.rvProjectItem?.visibility = View.VISIBLE
            } else {
                mViewDataBinding?.ivEmpty?.visibility = View.VISIBLE
                mViewDataBinding?.rvProjectItem?.visibility = View.GONE
            }
        }
        mViewDataBinding?.rvProjectItem?.adapter = contentAdapter
        mViewDataBinding?.rvProjectItem?.layoutManager =
            LinearLayoutManager(this.context)
        mViewDataBinding?.rvProjectItem?.addItemDecoration(SpacesItemDecoration(30))
        mViewModel?.projectContentLiveData?.observe(this) {
            it.onSuccess {
                contentAdapter.setListData(it)
            }
        }
        mViewModel?.showProjectCategoryContent(294)
    }
}